package com.base.cn.platform.os.controller.course.cou.play.exercises;

import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.cou.play.PlayCourseService;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import com.base.cn.platform.os.service.exam.record.LoadStartExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 随堂练习Controller
 *
 * @author s.li
 * @create 2018-08-07-17:57
 */
@RestController
@RequestMapping("/api/play/exercises")
public class ApiCourseExercisesController extends ApiBaseController {

    @Autowired
    private PlayCourseService playCourseService;
    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private LoadStartExamService loadStartExamService;

    /**
     * 加载随堂练习
     * @param userId 用户ID
     * @param courseId 购买的课程或套餐ID
     * @param catalogId 目录ID
     * @return Map<String,Object>
     */
    @RequestMapping("/listInClassExercise")
    public Map<String,Object> checkExercises(@RequestParam("userId") BigDecimal userId,
                                             @RequestParam("courseId") BigDecimal courseId,
                                             @RequestParam("catalogId") BigDecimal catalogId){
        Map<String,Object> resultMap = playCourseService.checkExercises(userId,courseId,catalogId);
        if(!ResultUtil.getSuccess(resultMap)){
            return resultMap;
        }
        BigDecimal examRecordId = new BigDecimal(ResultUtil.getEntity(resultMap).toString());
        Map<String,Object> condition = new HashMap<>();
        condition.put("id",examRecordId);
        condition.put("userId",userId);
        Map<String,Object> dataMap = loadStartExamService.loadExamRecord(condition);
        if(ResultUtil.getSuccess(dataMap)){
            return ResultUtil.SUCCESS(ResultUtil.getEntity(dataMap));
        }
        return dataMap;
    }


    /**
     * 保存随堂练习
     * @param examRecordId 考试记录ID
     * @param userId 用户ID
     * @param courseId 课程ID
     * @param catalogId 目录ID
     * @return 随堂练习结果
     */
    @RequestMapping("/saveExercises")
    public Map<String,Object> saveExercises(@RequestParam("examRecordId") BigDecimal examRecordId,
                                            @RequestParam("userId") BigDecimal userId,
                                            @RequestParam("courseId") BigDecimal courseId,
                                            @RequestParam("catalogId") BigDecimal catalogId){
        return coursePlayRecordService.saveExercises(examRecordId,courseId,catalogId,userId);
    }

}
